import 'dart:convert';
import 'dart:ui';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:http/http.dart';
import 'package:jet_rails/jr_constant/jr_urls.dart';
import 'package:jet_rails/jr_services/jr_apis.dart';
import 'package:jet_rails/jr_services/jr_services.dart';
import 'package:jet_rails/jr_utils/jr_logger.dart';

class JrTranslate {
  static Future<String?> translate(String content) async {
    try {
      EasyLoading.show(dismissOnTap: true);
      final res = await post(Uri.parse(JrUrls.translation), body: {
        "key": JrServices.instance.googleKey,
        "target": window.locale.languageCode,
        "q": content,
        "format": 'text'
      });
      if (res.statusCode == 200) {
        return json.decode(res.body)['data']['translations'][0]['translatedText'];
      }
    } catch (e) {
      JrLogger.e('translate error: $e');
    } finally {
      EasyLoading.dismiss();
    }

    return null;
  }
}
